<template>
	<view>
		<Address :address="address"></Address>
		<Locality></Locality>
		<Content :localdata="localdata" v-if="!lodingList"></Content>
		<view class="publish">
			<image src="../../static/tab/fab.png" mode="widthFix" @click="travels()"></image>
		</view>
		<load-list v-if="lodingList"></load-list>
		<none-data v-if="nonedata"></none-data>
	</view>
</template>

<script>
	import Address from './components/address.vue'
	import Content from './components/content.vue'
	import Locality from './components/locality.vue'
	import {
		addressdata
	} from '../../common/list.js'
	import {
		mapState
	} from 'vuex'
	var db = wx.cloud.database()
	var listdata = db.collection('userdata')
	export default {
		name: 'strategy',
		components: {
			Address,
			Content,
			Locality,
		},
		data() {
			return {
				address: '',
				cityion: '',
				addressData: '',
				localdata: [],
				lodingList:false,
				nonedata:false
			}
		},
		methods: {
			event(name) {
				this.lodingList = true
				this.nonedata = false
				console.log(name)
				if (name == "全部") {
					this.cityData(this.address)
				} else {
					let city = this.address
					this.tabCity(city, name)
				}
			},
			tabCity(city, name) {
				listdata.where({
						datainfo: {
							address: city,
							classdata: name
						}
					})
					.get()
					.then((res) => {
						let citydata = res.data
						this.resultCity(citydata)
						console.log("取出景点，全部，美食，网红打卡，其中一个分类：", res)
					})
					.catch((err) => {
						console.log(err)
					})
			},
			//获取当前位置
			addRess() {
				addressdata()
					.then((res) => {
						this.address = res.result.ad_info.city
						this.cityData(this.address)
					}).catch((err) => {
						this.address = "用户拒绝定位"
					})
			},
			cityData(city) {
				listdata.where({
						datainfo: {
							address: city
						}
					})
					.get()
					.then((res) => {
						console.log(res)
						// 筛选
						let citydata = res.data
						this.resultCity(citydata)
					})
					.catch((err) => {
						console.log(err)
					})
			},
			travels() {
				uni.navigateTo({
					url: '../travels/travels'
				})
			},
			resultCity(citydata) {
				var adddata = citydata.map((item) => {
					let id = item._id
					let datainfo = item.datainfo
					return {
						id,
						datainfo
					}
				})
				this.localdata = adddata
				console.log(adddata)
				this.lodingList = false
				if(this.localdata == ''){
					this.nonedata = true
				}
			}
		},
		onLoad() {
			this.addRess()
		},
		computed: {
			...mapState(['city']),
			count() {
				this.addressData = this.city.citying
			}
		},
		watch: {
			addressData(newValue, oldValue) {
				this.address = newValue
				this.cityData(newValue)
			}
		}
	}
</script>

<style>
	.publish image {
		width: 120upx !important;
		height: 100upx !important;
		border-radius: 40upx;
	}

	.publish {
		position: fixed;
		bottom: 20upx;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
	}
</style>
